﻿
using DataContext;
using Utils;
using System.Linq;
using EntityBLL;
using System.Collections.Generic;
namespace WorkFlowBLL
{
    public class CtrUser
    {                
        /// <summary>
        /// Check đăng nhập User
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool CheckExistUser(string userName, string password)
        {                        
            int? ret = 0;
            DAL.UserInstance.CreateInstance().uspAdminCheckLogin(userName, password, ref ret);
            if (ret != null) return (int)ret == 1;
            return false;
        }

        /// <summary>
        /// Kiểm tra đã tồn tại User hay chưa
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool CheckExistUserLogin(string userName)
        {
            int? ret = 0;
            DAL.UserInstance.CreateInstance().uspUserCheckExists(userName, ref ret);
            if (ret != null) return (int)ret == 1;
            return false;
        }

        /// <summary>
        /// Lấy thông tin User đăng nhập hệ thống
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public uspUserGetInfoByNameResult UserGetInfoByName(string userName)
        {            
            return DAL.UserInstance.CreateInstance().uspUserGetInfoByName(userName).FirstOrDefault();            
        }

        /// <summary>
        /// Lấy danh sách người dùng hệ thốn
        /// </summary>
        /// <returns></returns>
        public List<uspUserGetAllResult> GetListUserLogin()
        {            
            return DAL.UserInstance.CreateInstance().uspUserGetAll().ToList();            
        }

        /// <summary>
        /// Thêm mới 1 User
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public int InsertUser(string userName, string password, string fullName)
        {            
            return DAL.UserInstance.CreateInstance().uspUserInsert(userName, password, fullName, true);
        }

        /// <summary>
        /// Cập nhật thông tin người dùng
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="fullName"></param>
        /// <returns></returns>
        public int UpdateUserInfoByName(string userName, string password, string fullName)
        {            
            return DAL.UserInstance.CreateInstance().uspUserUpdateByUserName(userName, password, fullName, true);
        }

        /// <summary>
        /// Xóa thông tin người dùng
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public int DeleteUserInfo(int userID)
        {            
            return DAL.UserInstance.CreateInstance().uspUserDeleteByUserID(userID);
        }
    }
}
